package org.gameis.furtal;

import org.gameis.furtal.server.network.protocol.HelloFilter;
import org.gameis.furtal.server.network.protocol.MessageFilter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: zvoykish
 * Date: 27/1/12
 * Time: 1:06 PM
 */
public class FurtalProtocol {
    private State state = State.WAITING;
    private final List<MessageFilter> filters;

    public FurtalProtocol() {
        filters = new ArrayList<MessageFilter>();
        filters.add(new HelloFilter());
    }

    public String processInput(String input) {
        String output = null;
        switch (state) {
            case WAITING:
                output = FurtalConstants.INITIAL_MESSAGE;
                state = State.STARTED;
                break;
            case STARTED:
                Iterator<MessageFilter> it = filters.iterator();
                while (output == null && it.hasNext()) {
                    MessageFilter filter = it.next();
                    output = filter.process(input);
                }
        }
        return output;
    }

    private enum State {
        WAITING,
        STARTED
    }
}
